//
//  SecondViewController.m
//  通讯录罚写2
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "Cat.h"
#import "DetailView2Controller.h"
#import "AddView2Controller.h"
#import "ChineseToPinyin.h"

@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableDictionary *catDic;
@property (nonatomic, retain) UITableView *tableV;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"通讯录";
    [self getData];
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(leftAction:)] autorelease];
    
    
}
// 监测移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    
    if (sourceIndexPath.section != proposedDestinationIndexPath.section) {
        return sourceIndexPath;
    } else {
        return proposedDestinationIndexPath;
    }
    
}

// 移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    
    NSString *key = [self.catDic.allKeys objectAtIndex:sourceIndexPath.section];
    NSMutableArray *arr = [self.catDic objectForKey:key];
    Cat *cat = [[arr objectAtIndex:sourceIndexPath.row] retain];
    [arr removeObjectAtIndex:sourceIndexPath.row];

    NSString *desKey = [self.catDic.allKeys objectAtIndex:destinationIndexPath.section];
    NSMutableArray *desArr = [self.catDic objectForKey:desKey];
    [desArr insertObject:cat atIndex:destinationIndexPath.row];
    [cat release];
    
    [self.tableV reloadData];
    
}


// 可移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 协议方法
- (void)addTocatDic:(Cat *)cat
{
    if (nil != cat) {
        NSString *key = [[ChineseToPinyin pinyinFromChiniseString:cat.name] substringToIndex:1];
        NSMutableArray *arr = nil;
        if ([self.catDic objectForKey:key] != nil) {
            arr = [self.catDic objectForKey:key];
            [arr addObject:cat];
            
        } else {
            arr = [NSMutableArray array];
            [arr addObject:cat];
            [self.catDic setObject:arr forKey:key];
            
        }
    }
    
    [self.tableV reloadData];
    
}
// 左按钮
- (void)leftAction:(UIBarButtonItem *)sender
{
    AddView2Controller *addVC = [[AddView2Controller alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}
// 可编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableV setEditing:editing animated:animated];
}
// 可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
// 编辑完成
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [self.catDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.catDic objectForKey:key];
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        [arr removeObjectAtIndex:indexPath.row];
        if (arr.count == 0) {
            [self.catDic removeObjectForKey:key];
        }
        [self.tableV reloadData];
    }
    
}
// 选中触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailView2Controller *detailVC = [[DetailView2Controller alloc] init];
    
    NSString *key = [self.catDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.catDic objectForKey:key];
    Cat *cat = [arr objectAtIndex:indexPath.row];
    detailVC.cat = cat;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
// 右侧
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.catDic.allKeys;
}
// 区标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.catDic.allKeys objectAtIndex:section];
}
// 区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.catDic.allKeys.count;
}
// 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.catDic.allKeys objectAtIndex:section];
    NSMutableArray *arr = [self.catDic objectForKey:key];
    return arr.count;
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
        
    }
    NSString *key = [self.catDic.allKeys objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.catDic objectForKey:key];
    Cat *cat = [arr objectAtIndex:indexPath.row];
    
    cell.textLabel.text = cat.name;
    cell.detailTextLabel.text = cat.phone;
    cell.imageView.image = [UIImage imageNamed:cat.image];
    return cell;
    
}

- (void)getData
{
    //解析路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HW" ofType:@"plist"];
    
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    self.catDic = [NSMutableDictionary dictionary];
    
    for (NSString *key in dic) {
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *tempDic in [dic objectForKey:key]) {
            Cat *cat = [[Cat alloc] init];
            cat.name = [tempDic objectForKey:@"name"];
            cat.phone = [tempDic objectForKey:@"phone"];
            cat.sex = [tempDic objectForKey:@"sex"];
            cat.age = [tempDic objectForKey:@"age"];
            cat.image = [tempDic objectForKey:@"image"];
            
            [arr addObject:cat];
            [cat release];
        }
        [self.catDic setObject:arr forKey:key];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based appliCation, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
